<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="45%"
      v-dialogDrag
      append-to-body
    >

      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        v-loading="formLoading"
        label-width="100px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="formData.sex">
            <el-radio
              v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话号码" prop="phoneNumber">
          <el-input v-model="formData.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="部门" prop="divisionId">
          <el-select v-model="formData.divisionId" placeholder="请选择部门">
            <el-option :label="item.name" v-for="item in bu" :key="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="指导教师" prop="teaName">
          <el-select v-model="formData.teaId" placeholder="请选择指导教师">
            <el-option
               :label="item.name" 
              v-for="item in teachers"
              :key="item.id"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="简介">
          <el-input
            v-model="formData.description"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as StudentApi from "@/api/yuangong/staff";
export default {
  name: "StudentForm",
  components: {},
  data() {
    return {
      // 教师列表
      teachers: [],
     
      // 部门列表
      bu: [
      ],
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: "5",
        name: "",
        teaName: "",
        sex: "",
        teaId: "",
        description: "",
        divisionId: "",
        phoneNumber: "",
        
      },
      // 教师列表
      teach: {
        pageSize: 10,
        pageNo: 1,
      },
      // 表单校验
      formRules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        sex: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        divisionId: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        teaId: [
          { required: true, message: "指导教师不能为空", trigger: "change" },
        ],
        phoneNumber: [
          { required: true, message: "电话号码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getteachers();
    this.getDept()
  },
  methods: {
    // 获取教师数据
    async getteachers() {
      try {
        this.loading = true;
        const res = await StudentApi.getteacherPage(this.teach);
        this.teachers = res.data.list;
        
      
      } finally {
        this.loading = false;
      }
    },
    async getDept(){
      const res = await StudentApi.getDeptList();
     
      this.bu=res.data;
    },
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await StudentApi.getStudent(id);
          this.formData = res.data;
          this.formData.teaName=res.data.teaName
         console.log(this.formData)
          this.title = "修改学生";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增学生";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await StudentApi.updateStudent(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit("success");
          return;
        }
        // 添加的提交
        await StudentApi.createStudent(this.formData);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit("success");
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: "",
        name: "",
        sex: "",
        teaName: "",
        description: "",
        divisionName: "",
      };
      this.resetForm("formRef");
    },
  },
};
</script>
